import { post, get, put, Delete, form } from "@/axios/http.js";
import { basicApi } from "@/api/index.js"
import { setCookie, removeCookie, getCookie } from "@/utils/auth";
//模块数据
export default {
    namespaced: true,
    state: {
        user: getCookie('user') ? JSON.parse(getCookie('user')) : {
            isLogin: false,
            userName: '',
            userId: 0,
            roleid: 0,
            userImg: '',
        },
        otherUserInfo: getCookie('otherUserInfo') ? JSON.parse(getCookie('otherUserInfo')) : {},
        userInfo: getCookie('userInfo') ? JSON.parse(getCookie('userInfo')) : {}
    },
    actions: {
        // 用户登录
        login({ commit }, params) {
            return get(basicApi.user.login, params).then(res => {
                setCookie('ACCESS_TOKEN', res.data.msg)
                    // setCookie('casSessionId', res.data.msg)
                return res
            });
        },
        // 用户登录
        findByUserIdInfo({ commit }) {
            var timeOut = 0
            var tokenId = setInterval(getToken, 200);

            function getToken() {
                if (getCookie("casSessionId") || timeOut > 3000) {
                    clearInterval(tokenId);
                    return get(basicApi.user.findByUserIdInfo).then(res => {
                        if (res && res.data.code === 200) {
                            commit('INIT_USER_DATA', res.data.result)
                            commit('INIT_USERINFO_DATA', res.data.result)
                        } else {
                            // this._vm.$message.error('获取用户信息失败,请重试!')
                        }
                        return res
                    });
                } else {
                    timeOut += 200
                }
            }

        },
    },
    mutations: {
        LOGOUT_CLEAR_USER(state) {
            removeCookie('ACCESS_TOKEN')
            removeCookie('user')
            removeCookie('userInfo')
            removeCookie('isManager')
            removeCookie('messageDot')
            removeCookie('otherUserInfo')
            removeCookie('casSessionId')
            removeCookie('group')
            removeCookie('articleOrPost')
            state.user = {
                isLogin: false,
                userName: '',
                userId: 0,
                userImg: '',
            }
        },
        INIT_USER_DATA(state, params) {
            state.user = {
                isLogin: true,
                userName: params.username,
                userId: params.id,
                userImg: '', //头像暂时不传
                roleid: params.roleid,
            }
            setCookie('user', state.user)
        },
        INIT_USERINFO_DATA(state, params) {
            state.userInfo = params
            setCookie('userInfo', state.userInfo)
        },
        INIT_OTHERUSERINFO_DATA(state, params) {
            state.otherUserInfo = params
            setCookie('otherUserInfo', state.otherUserInfo)
        }
    },
};